﻿package svl.gui 
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.TextField;		

	/**
	 * @author sanderl
	 */
	public class ButtonDisplayState extends Sprite 
	{
		private var matrix:Matrix;
		private var colors:Array;
		private var roundness:int;
		private var newWidth:int;
		private var newHeight:int;
		private var label:TextField;
		private var newMargin:Number;
		private var background:Sprite = new Sprite();
		private var textOffset:Number = 6;
		
		public function ButtonDisplayState(textLabel:TextField, fillCollors:Array, height:int, margin:Number, r:int)
		{
			newMargin = margin;
			label = textLabel;
			
			newWidth = newMargin + label.textWidth + newMargin + textOffset;
			newHeight = height;
			
			matrix = new Matrix();
			matrix.createGradientBox(newWidth, newHeight, Math.PI/2, 0, 0);
			
			colors = fillCollors;
			roundness = r;
			
			draw();
		}
		
		private function draw():void
		{
			background.graphics.beginGradientFill(GradientType.LINEAR, colors, [1,1], [newHeight,255], matrix);
			background.graphics.drawRoundRect(0, 0, newWidth, newHeight, roundness, roundness);
			background.graphics.endFill();
			addChild(background);
			
			var grid:Rectangle = new Rectangle(roundness,roundness,newWidth-(roundness*2),newHeight-(roundness*2));
			background.scale9Grid = grid;
			
			addChild(label);
		}
		
		public function set text(value:String):void
		{
			label.text = value;
			background.width = newMargin + label.width + newMargin;
		}
	}
}
